<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="/backend/css/pintuer.css">
    <link rel="stylesheet" href="/backend/css/admin.css">
    <script src="/backend/js/jquery.js"></script>
    <script src="/backend/js/pintuer.js"></script>
    <script src="/backend/js/vuejs-2.5.16.js"></script>
    <script src="/backend/js/axios-0.18.0.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/backend/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="/backend/element-ui/lib/index.js"></script>
    <script src="/backend/js/verify.js"></script>


</head>
<body>
<div class="panel admin-panel" id="questionnairesListDiv">
    <div class="panel-head"><strong class="icon-reorder"> 问卷列表</strong> <a href="" style="float:right; display:none;">添加字段</a>
    </div>
    <div class="padding border-bottom">
        <form>
            <ul class="search" style="padding-left:10px;">
                <li><a class="button border-main icon-plus-square-o" href="/backend/questionnaires_add.html"> 添加问卷</a></li>
                <li>搜索：</li>

                <li>
                    <input type="text" placeholder="请输入搜索卷卷名称" name="name" class="input" v-model="name"
                           style="width:250px; line-height:17px;display:inline-block"/>
                    <a class="button border-main icon-search" @click="search()"> 搜索</a></li>
            </ul>
        </form>
    </div>
    <table class="table table-hover text-center">
        <tr>
            <th width="5%">选择</th>
            <th>序号</th>
            <th>封面</th>
            <th width="10%">等级</th>
            <th>问卷星别</th>

            <th width="10%">问问时间</th>
            <th width="310">操作</th>
        </tr>

        <tr v-for="(questionnaires,index) in pagination.records">
            <td style="text-align:left; padding-left:10px;"><input type="checkbox" name="ids" :value=questionnaires.id
                                                                   v-model="checkList"/></td>
            <td>{{index}}</td>
            <td width="10%"><img :src="questionnaires.cover" alt="" width="70" height="50"/></td>
            <td>{{questionnaires.level}}</td>
            <td>{{questionnaires.star}}</td>

            <td>
                {{questionnaires.updated}}
            </td>
            <td>
                <div class="button-group">
                    <a class="button border-main" :href="'/backend/questionnaires_add.html?id='+questionnaires.id"><span
                            class="icon-edit"></span>修改</a>
                    <a class="button border-red" href="javascript:void(0)" @click="deleteHandle(null,questionnaires.id)">
                        <span class="icon-trash-o"></span> 删除</a>
                </div>
            </td>
        </tr>

        <tr>
            <td colspan="7" style="text-align:left;padding-left:20px;">
                <a href="javascript:void(0)" class="button border-red icon-trash-o" style="padding:5px 15px;"
                   @click="deleteHandle('批量', null)"> 批量删除</a>
                <span>总记录数：{{pagination.total}}</span>
            </td>
        </tr>
        <tr>
            <td colspan="8">
                <div class="pagelist">
                    <!--分页组件-->
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.pageNum"
                            :page-size="pagination.pageSize"
                            :total="pagination.total"
                            background
                            layout="prev, pager, next">
                    </el-pagination>
                </div>
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    const app = new Vue({
        el: '#questionnairesListDiv',
        data: {
            pagination: { // 分页对象
                pageNum: 1,
                pageSize: 5,
                total: 20,
                records: []
            },
            name: '',
            checkList: [],
            ids:[]
        },
        methods: {

            search(){
                this.pagination.pageNum=1;
                this.pagination.pageSize=5;
                this.findByPage();
            },
            // 分页查询
            findByPage() {
                const params = {
                    pageNum: this.pagination.pageNum,//当前页码
                    pageSize: this.pagination.pageSize,//每页大小
                    name: this.name//问卷名称
                };
                axios.post('/questionnaires/page', {params: params},{headers:{
                        contentType: "application/json;charset=UTF-8",
                    'Authorization':localStorage.getItem('Authorization')
                    }}).then(resp => {
                    let resultInfo = resp.data;
                    if (resp.status=200) {
                        this.pagination.total = resp.data.total;this.pagination.records = resp.data.records;
                        this.pagination.pageSize = resp.data.size;this.pagination.pageNum = resp.data.current;
                    } else {
                        alert(resp.status);
                    }
                })
            },
            //当前页改变时调用的方法
            handleCurrentChange(pageNum) {
                //将最新页码赋值给分页对象的当前页码
                this.pagination.pageNum = pageNum;
                //重新发起分页请求
                this.findByPage();
            },
            //改变每页大小时调用的方法
            handleSizeChange(pageSize) {
                //将最新的每页大爱小赋值给分页对象的当前页码
                this.pagination.pageSize = pageSize;
                //重新发起分页请求
                this.findByPage();
            },
            // 删除
            deleteHandle(type, id) {
                if (type === '批量' && id === null) {
                    if (this.checkList.length === 0) {
                        return this.$message.error('请选择删除对象')
                    }
                }
                this.$confirm('确认删除该问卷, 是否继续?', '确定删除', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                }).then(() => {
                    this.ids = type === '批量' ? this.checkList.join(',') : id;
                    axios.post('/questionnaires/delete',this.ids,{headers:{
                            'Content-Type':"application/json",
                            'Authorization':localStorage.getItem('Authorization')
                    }}).then(resp => {
                        let resultInfo = resp.data;
                        if (resp.status == 200) {
                            let num = type === '批量' ? this.checkList.length : 1;
                            console.log(num);
                            const pages = Math.ceil((this.pagination.total - num) / this.pagination.pageSize) // 计算总页数
                            this.pagination.pageNum = this.pagination.pageNum > pages ? pages : this.pagination.pageNum; // 修正当前页
                            this.pagination.pageNum = this.pagination.pageNum < 1 ? 1 : this.pagination.pageNum; // 修正当前页
                            this.checkList = [];
                            this.findByPage();
                        } else {
                            alert(resultInfo.msg);
                        }
                    })
                })
            },


        },
        created() {
            this.findByPage();
        }
    })
</script>
</body>
</html>